package com.ruoyi.project.setting.security.utils;

import com.ruoyi.project.setting.security.LoginUser;
import com.ruoyi.project.setting.security.service.TokenService;
import net.dreamlu.mica.core.spring.SpringContextUtil;
import net.dreamlu.mica.core.utils.$;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author dy
 */
public class UserUtils {

    /**
     * 获取登陆用户
     **/
    public static LoginUser currentLoginUser() {
        return Objects.requireNonNull(SpringContextUtil.getBean(TokenService.class)).currentLoginUser();
    }

    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }


    /**
     * 取得该部门的租户ID
     *
     * @param deptId    租户ID
     * @param ancestors 父级列表
     * @return 该部门租户ID
     */
    public static Long turnIntoTenantId(Long deptId, String ancestors) {
        if (ancestors.equals("0")) {
            return deptId;
        }
        List<String> deptIds = Arrays.asList($.split(ancestors, ","));
        // 有父级节点
        if (deptIds.size() > 1) {
            return Long.parseLong(deptIds.get(1));
        }
        return deptId;
    }
}
